//
// Created by zzc on 18-1-4.
//

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmockery.h>
#include "../src/adlist.h"

void test_create(void **state)
{
    list *list = listCreate();
    assert_int_equal(0, listLength(list));
    assert_true(NULL == listFirst(list));
    listRelease(list);
}

void test_addHead(void **state)
{
    list *list = listCreate();
    int a = 1;
    listAddNodeHead(list, &a);
    assert_int_equal(1, *(int *)(listFirst(list)->value));
    int b = 2;
    listAddNodeHead(list , &b);
    assert_int_equal(2, *(int *)(listFirst(list)->value));

    listRelease(list);
}

void test_addTail(void **state)
{
    list *list = listCreate();
    int a = 1;
    listAddNodeTail(list, &a);
    assert_int_equal(1, *(int *)(listFirst(list)->value));
    int b = 2;
    listAddNodeTail(list , &b);
    assert_int_equal(1, *(int *)(listFirst(list)->value));
    assert_int_equal(2, *(int *)(listTail(list)->value));

    listRelease(list);
}

void test_insert(void **state)
{
    list *list = listCreate();
    int a = 1;
    listAddNodeHead(list, &a);
    int b = 2;
    listAddNodeTail(list , &b);

    int c = 3;

    listInsertNode(list, listFirst(list), &c, 1);

    assert_int_equal(3, listLength(list));

    listRelease(list);
}

void test_index(void **state)
{
    int arr[] = {1,2,3,4,5,6};
    int len = 6;
    list *l = listCreate();
    for(int i = 0; i < len; i++) {
        listAddNodeTail(l, &arr[i]);
    }

    listNode *t = listIndex(l, 0);
    assert_int_equal(1, *(int *)t->value);

    t = listIndex(l, -1);
    assert_int_equal(6, *(int *)t->value);

    t = listIndex(l, 1);
    assert_int_equal(2, *(int *)t->value);

    listRelease(l);
}


void test_search(void **state)
{
    int arr[] = {1,2,3,4,5,6};
    int len = 6;
    list *l = listCreate();
    for(int i = 0; i < len; i++) {
        listAddNodeTail(l, &arr[i]);
    }


    listNode *node = listSearchKey(l, &arr[0]);
    assert_int_equal(1, *(int *)(node->value));

    node = listSearchKey(l, &arr[3]);
    assert_int_equal(4, *(int *)(node->value));

    int tt = 1;

    node = listSearchKey(l, &tt);
    assert_true(node == NULL);

    listRelease(l);
}


int main(int argc, char *argv[])
{
    const UnitTest tests[] = {
            unit_test(test_create),
            unit_test(test_addHead),
            unit_test(test_addTail),
            unit_test(test_insert),
            unit_test(test_index),
            unit_test(test_search)


    };
    return run_tests(tests);
}